#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/wdt.h>
#include "main.h"
#include "types.h"
/*#include "lcd.h"*/
/*#include "eeprom_fifo.h"*/
/*/#include "enc28j60.h"*/
/*#include "vs.h"*/

UINT32 get_acttime(void);

volatile UINT32 time;
volatile byte run_timerservice;
static byte mymac[6] = {0x00,0x55,0x58,0x10,0x50,0x11};

int main(void)
{
	time = 0;
	run_timerservice = 0;
	
  // init ports
  PORTA = B0 | B1 | B2 | B3 | B4 | B5 | B6 | B7;
  DDRA  = 0x00;
  PORTB = B0 | B1 | B2 | ~B3| B4 | ~B5| B6 | B7;
  DDRB	= B0 | B1 | B2 | B3 | ~B4| B5 | ~B6| B7;
  PORTC	= B0 | B1 | B2 | B3 | B4 | B5 | B6 | B7;
  DDRC	= B0 | B1 | B2 | B3 | B4 | B5 | B6 | B7;
  PORTD	= B0 | B1 | B2 | B3 | B4 | B5 | B6 | B7;
  DDRD	= B0 | B1 | ~B2| ~B3| ~B4| ~B5| ~B6| B7;
  
  // init watchdog
  
  //init timer
	TCNT1   = 0x00;
	TCCR1A  = 0;
	TCCR1B  = (1<<WGM12)|(1<<CS12)|(0<<CS11)|(1<<CS10);
	OCR1A   = (F_CPU/1024)-1;
	TIMSK1 |= (1<<OCIE1A);
  
  // init spi
  SPCR = (1<<SPE)|(1<<MSTR); 			//enable SPI, Master, clock=f_cpu/4
  SPSR = (1<<SPI2X); 				//x2 -> clock=f_cpu/2
  
  // init LCD
  //lcd_init();
  
  // init eeproms
  //fifo_init();
  
  // init VS1053
  //VS1053_Init();
  
  
  // init enc28j60
  //ENC28J60_Init( mymac );
  
	for(;;)
	{
		if( run_timerservice )
		{
			run_timerservice = 0;	
		}
		LED_ON();
	}
	
	return 0;
}


ISR( TIMER1_COMPA_vect, ISR_BLOCK )
{
	time++;
	run_timerservice = 1;
	
	return;
}


UINT32 get_acttime(void)
{
  UINT32 i;
  
  cli();
  i = time;
  sei();
  
  return i;
}